﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dpptask2.Exceptions;

namespace dpptask2.OptionClasses
{
    public class optionIntAttribute : optionParametrizedAttribute
    {

        public int min = int.MinValue;
        public int max = int.MaxValue;

        public override Type typeAccepted()
        {
            if (choiceRequired)
                return typeof(int);
            else
                return typeof(int?);
        }

        public override void check()
        {
            base.check();
            checkDefaultValue();
        }

        private void checkDefaultValue()
        {
            if (defaultValue == null)
                return;

            if ((int)defaultValue < min || (int)defaultValue > max)
                throw new ParserException("defaultValue {0} is out of range ({1} to {2}).", defaultValue, min, max);
        }

        public override object createValueFromString(string stringValue)
        {
            int value;
            if (!int.TryParse(stringValue, out value))
                throw new ParserException("Cannot convert {0} to integer value.", stringValue);


            if (value < min || value > max)
                throw new ParserException("{0} is out of range ({1} to {2}).", value, min, max);

            return value;
        }
    }
}
